﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Shp.Web.UI.WebControls.Iconized.Designers;
using Shp.Web.UI.WebControls.Iconized.Editors;

namespace Shp.Web.UI.WebControls.Iconized
{
   [Designer(typeof(IconizedButtonDesigner))]
   [DefaultProperty("Text")]
   // ToolboxItem Attribute appears to throw an exception when dragging the control from the toolbox to the WebForm - 'The operation could not be completed. Invalid FORMATETC structure.'
   // found this issue late, bc I never use drag and drop... :-O
   // leaving this comment as a reference, so I don't spend two hours troubleshooting the exception.
   //[ToolboxItem(true)]
   [ToolboxBitmap(typeof(ResourceFinder), "Shp.Web.UI.WebControls.Iconized.assets.im.dt.toolbox.bmp")]
   [ToolboxData("<{0}:IconizedButton runat=server></{0}:IconizedButton>")]   
   public class IconizedButton : LinkButton, IIconizedButton
   {
      #region Fields
      private IconizedSharedBase _iconizedUtility;
      #endregion Fields

      #region Properties
      [Bindable(true)]
      [Category("Iconized")]
      [DefaultValue("Gray")]
      [Editor(typeof(ButtonSkinUITypeEditor), typeof(UITypeEditor))]
      [Description("The button's Skin - button's colors.")]
      public ButtonSkinType ButtonSkin
      {
         get { return this.IconizedUtility.ButtonSkin; }
         set { this.IconizedUtility.ButtonSkin = value; }
      }

      [Bindable(true)]
      [Category("Iconized")]
      [DefaultValue("")]
      [Description("Custom CSS Class.  Use this to add Custom Skinning and other Custom CSS to the control.")]
      public string CustomCssClass
      {
         get { return this.IconizedUtility.CustomCssClass; }
         set { this.IconizedUtility.CustomCssClass = value; }
      }

      [Bindable(true)]
      [Category("Iconized")]
      [DefaultValue("None")]
      //[TypeConverter(typeof(IconTypeConverter))]
      [Editor(typeof(IconUITypeEditor), typeof(UITypeEditor))]
      [Description("The type (image) of the icon.  Works in conjuction with the IconPosition property.")]
      public IconType IconType
      {
         get { return this.IconizedUtility.IconType; }
         set { this.IconizedUtility.IconType = value; }
      }

      [Bindable(true)]
      [Category("Iconized")]
      [DefaultValue("")]
      [Description("The CSS Class of the custom icon.  IconType must equal IconType.Custom or this will be ignored.")]
      public string CustomIconType
      {
         get { return this.IconizedUtility.CustomIconType; }
         set { this.IconizedUtility.CustomIconType = value; }
      }

      [Bindable(true)]
      [Category("Iconized")]
      [DefaultValue("None")]
      [Description("The position of the icon.  Works inconjuction with the IconType property.")]
      public IconPositionType IconPosition
      {
         get { return this.IconizedUtility.IconPosition; }
         set { this.IconizedUtility.IconPosition = value; }
      }

      [Bindable(true)]
      [Category("Iconized")]
      [DefaultValue("true")]
      [Description("Whether (or not) to display the background style on mouse out (or non-mouse over).")]
      public bool DisplayMouseOutStyle
      {
         get { return this.IconizedUtility.DisplayMouseOutStyle; }
         set { this.IconizedUtility.DisplayMouseOutStyle = value; }
      }

      [Bindable(true)]
      [Category("Iconized")]
      [DefaultValue("false")]
      [Description("Add Round Corners to Button..")]
      public bool RoundCorners
      {
         get { return this.IconizedUtility.RoundCorners; }
         set { this.IconizedUtility.RoundCorners = value; }
      }

      #region Hidden Base Properties
      [Browsable(false)]
      public override string CssClass
      {
         get { return base.CssClass; }
         set { base.CssClass = value; }
      }
      #endregion Hidden Base Properties

      /// <summary>
      /// Gets the current HttpContext.
      /// </summary>
      /// <value>The current context.</value>
      public HttpContext CurrentContext
      {
         get { return this.Context; }
      }

      /// <summary>
      /// Gets a dictionary of state information that allows you to save and restore
      /// the view state of a server control across multiple requests for the same
      /// page.
      /// </summary>
      /// <value>The control's ViewState.</value>
      public StateBag ControlViewState
      {
         get { return this.ViewState; }
      }

      /// <summary>
      /// Gets the iconized utility.  This acts as a base for iconized buttons providing shared/common members.
      /// </summary>
      /// <value>The iconized utility.</value>
      internal IconizedSharedBase IconizedUtility
      {
         get { return this._iconizedUtility; }
      }
      #endregion Properties

      #region Methods
      /// <summary>
      /// Renders the contents.
      /// </summary>
      /// <param name="output">The output.</param>
      protected override void RenderContents(HtmlTextWriter output)
      {
         // add the button icon span element and respective classes if the IconType != IconType.None
         if (this.IconType != IconType.None)
         {
            output.AddAttribute(HtmlTextWriterAttribute.Class, string.Format("shp-ico {0}",
               (this.IconType != IconType.Custom) ? this.IconType.ToString("G") : this.CustomIconType));
            output.RenderBeginTag(HtmlTextWriterTag.Span);
            output.RenderEndTag();
         }
         output.Write(this.Text);
      }
      #endregion Methods

      #region Event Handlers
      /// <summary>
      /// Raises the <see cref="E:System.Web.UI.Control.PreRender"/> event.
      /// </summary>
      /// <param name="e">An <see cref="T:System.EventArgs"/> object that contains the event data.</param>
      protected override void OnPreRender(EventArgs e)
      {
         this.IconizedUtility.RegisterStyles();
         this.IconizedUtility.BuildCssClass();
         base.OnPreRender(e);
         // RenderContents is called by the pipeline here - after OnPreRender event.
      }
      #endregion Event Handlers

      #region Ctors
      /// <summary>
      /// Initializes a new instance of the <see cref="IconizedButton"/> class.
      /// </summary>
      public IconizedButton()
         : base()
      {
         this._iconizedUtility = new IconizedSharedBase(this);
      }
      #endregion Ctors
   }   
}